package com.yuxi.crystal.animation.viewAnimation;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.RotateAnimation;
import android.widget.SeekBar;
import android.widget.TextView;

import com.yuxi.crystal.animation.R;

public class ViewAnimationActivity extends AppCompatActivity {
    SwitchCompat scaleCenterSwitch;
    SeekBar rotateSeekBar;
    SeekBar rotateSeekBarHeight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_animation);
        final View view = findViewById(R.id.imageView);
        View btnAlpha= findViewById(R.id.buttonAlepha);
        scaleCenterSwitch = (SwitchCompat) findViewById(R.id.swithAlphaCenter);
        View btnRotate = findViewById(R.id.btnSwitch);
        rotateSeekBar = (SeekBar) findViewById(R.id.rotateSeekBar);
        rotateSeekBarHeight = (SeekBar)findViewById(R.id.rotateSeekBarHeight);
        final float scale = getResources().getDisplayMetrics().density;
        final TextView tvXProcess = (TextView) findViewById(R.id.tvXProcess);
        final TextView tvYProcess = (TextView)findViewById(R.id.tvYProcess);
        final  View ivRotateCenter = findViewById(R.id.pointPivotCenter);
        final View pointStartView = findViewById(R.id.pointStartView);
        final ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.layoutViewAnimation);
        btnAlpha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
                alphaAnimation.setDuration(1000);
                view.startAnimation(alphaAnimation);
            }
        });
        btnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean haveCenter = scaleCenterSwitch.isChecked();
                if(haveCenter){
                    RotateAnimation animation = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5F,RotateAnimation.RELATIVE_TO_SELF,0.5F);
                    animation.setDuration(1000);
                    view.startAnimation(animation);
                }else {
                    RotateAnimation animation = new RotateAnimation(0,360,rotateSeekBar.getProgress()*scale,rotateSeekBarHeight.getProgress()*scale);
                    animation.setDuration(1000);
                    view.startAnimation(animation);
                }
            }
        });
        ConstraintLayout.LayoutParams viewLayoutParams = (ConstraintLayout.LayoutParams) view.getLayoutParams();
        final int startMargin = viewLayoutParams.leftMargin;
        final int topMargin = viewLayoutParams.topMargin;
        int pointWidth = viewLayoutParams.width;
        final int pointRadius = pointWidth/2;
        final ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(constraintLayout);
        rotateSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvXProcess.setText(progress+"dp");
                constraintSet.connect(R.id.pointPivotCenter,ConstraintSet.START,ConstraintSet.PARENT_ID,ConstraintSet.START,(int)(startMargin+progress*scale-pointRadius));
                constraintSet.applyTo(constraintLayout);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        rotateSeekBarHeight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvYProcess.setText(progress+"dp");
                constraintSet.connect(R.id.pointPivotCenter,ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP,(int)(topMargin+progress*scale-pointRadius));
                constraintSet.applyTo(constraintLayout);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
}
